package com.app.couponsharelife.ui.main.nativelife.order.fragment

import android.graphics.drawable.AnimationDrawable
import android.os.Bundle
import androidx.constraintlayout.widget.ConstraintLayout
import androidx.recyclerview.widget.LinearLayoutManager
import com.app.couponsharelife.R
import com.app.couponsharelife.base.LazyFragment
import com.app.couponsharelife.data.bean.OrderClassBean
import com.app.couponsharelife.data.bean.ShopOrderListBean
import com.app.couponsharelife.ui.adapter.ShopOrderListAdapter
import com.scwang.smartrefresh.layout.api.RefreshFooter
import com.scwang.smartrefresh.layout.api.RefreshHeader
import com.scwang.smartrefresh.layout.api.RefreshLayout
import com.scwang.smartrefresh.layout.constant.RefreshState
import com.scwang.smartrefresh.layout.listener.OnMultiPurposeListener
import kotlinx.android.synthetic.main.base_refresh_head.*
import kotlinx.android.synthetic.main.fragment_shop_orderlist.*

/**
 *Created by xct on 2019/3/13
 *Description:本地生活订单列表页面
 */
class ShopOrderListFragment:LazyFragment() {
    private var myAdapter: ShopOrderListAdapter?=null
    private val dataList= arrayListOf<ShopOrderListBean>()
    private var pageIndex=1
    companion object {
        fun newInstance(orderClassBean: OrderClassBean): ShopOrderListFragment {
            val fragment = ShopOrderListFragment()
            val bundle= Bundle()
            bundle.putParcelable("Data",orderClassBean)
            fragment.arguments =bundle
            return fragment
        }
    }
    override val viewLayoutId: Int
        get() = R.layout.fragment_shop_orderlist

    override fun initViews() {
        setRefresh()
        setData()
        myAdapter=ShopOrderListAdapter(context!!,dataList)
        shopOrderlist_rv.layoutManager=LinearLayoutManager(context)
        shopOrderlist_rv.adapter=myAdapter
    }
    private fun setRefresh(){
        hotsale_refreshimg.setImageResource(R.drawable.anim_refresh)
        val animationDrawable=hotsale_refreshimg.drawable as AnimationDrawable
        shopOrderlist_smart.setOnMultiPurposeListener(object : OnMultiPurposeListener {
            override fun onFooterMoving(footer: RefreshFooter?, isDragging: Boolean, percent: Float, offset: Int, footerHeight: Int, maxDragHeight: Int) {

            }

            override fun onHeaderStartAnimator(header: RefreshHeader?, headerHeight: Int, maxDragHeight: Int) {

            }

            override fun onFooterReleased(footer: RefreshFooter?, footerHeight: Int, maxDragHeight: Int) {

            }

            override fun onStateChanged(refreshLayout: RefreshLayout, oldState: RefreshState, newState: RefreshState) {

            }

            override fun onHeaderMoving(header: RefreshHeader?, isDragging: Boolean, percent: Float, offset: Int, headerHeight: Int, maxDragHeight: Int) {
                setAnima(percent)
            }

            override fun onFooterFinish(footer: RefreshFooter?, success: Boolean) {

            }

            override fun onFooterStartAnimator(footer: RefreshFooter?, footerHeight: Int, maxDragHeight: Int) {

            }

            override fun onHeaderReleased(header: RefreshHeader?, headerHeight: Int, maxDragHeight: Int) {
                animationDrawable.start()
            }

            override fun onLoadMore(refreshLayout: RefreshLayout) {
                //getGoodsList(false)
                shopOrderlist_smart.finishLoadMore(2000)
            }

            override fun onRefresh(refreshLayout: RefreshLayout) {
                pageIndex=1
                //getGoodsList(true)
                shopOrderlist_smart.finishRefresh(2000)
            }

            override fun onHeaderFinish(header: RefreshHeader?, success: Boolean) {
                hotsale_refreshimg.setImageResource(R.drawable.refresh_0)
                animationDrawable.stop()
            }
        })
    }
    private fun setAnima(vlaue:Float){
        val layoutParams=hotsale_refreshimg.layoutParams as ConstraintLayout.LayoutParams
        layoutParams.matchConstraintPercentHeight=vlaue/2
        layoutParams.dimensionRatio="0.875:1"
        hotsale_refreshimg.layoutParams=layoutParams
    }
    override fun lazyLoad() {

    }
    private fun setData(){
        for (i in 0 until 15){
            val shopOrderListBean=ShopOrderListBean(i.toString(),"1","1")
            dataList.add(shopOrderListBean)
        }
    }
}